################################################################################################
# This script takes the QV/SV results and generates eight histograms (as in the Columbia 
# Elections paper) and eight CDFs for the subject strength of preferences. These are as follows: 
# 
# - Take bins of size 10 and assign a positive (negative) sign to points attributed to a 
#   proposal the subject favors (is against).  
# - Then construct the histograms for each proposal (x4) and for each condition (x2) with an 
#   absolute count of how many subjects fall on each bin.  
# - All subjects who voted InFavor/Against but gave 0 points were given +/- 1 accordingly.
# - All abstentions are placed at 0, even if they gave them points. But a note has been 
#   included to isolate the number of abstentions (on the side).
# 
# Exports the eight histograms to the /California Stage 2 - Experiment Plots/Sample Preference Intensity Histograms folder. 
# Exports the eight CDF plots to the /California Stage 2 - Experiment Plots/Sample Preference Intensity CDF folder.
# 
# Author: Luis S.
# Last Modified: 7.23.16
################################################################################################

library(dplyr)
library(ggplot2)

# setwd("C:/Users/Luis/Dropbox/Research/Casella/Storable Votes (MTurk) - California Experiment/California Experiment Results/")

######################################
##### Histrogram & CDF Functions #####
######################################

ProposalHist <- function(Sample_Data, Sample_Summary, Proposal, SampleTitle)
{
  #--- Set Graphing Parameters
  
  binWidth <- 10
  plotMax <- 70
  
  bins_width10 <- c("[-100,-90)", "[-90,-80)", "[-80,-70)", "[-70,-60)", "[-60,-50)", "[-50,-40)", 
                    "[-40,-30)", "[-30,-20)", "[-20,-10)", "[-10,0)", "0", "(0,10]", "(10,20]", 
                    "(20,30]", "(30,40]", "(40,50]", "(50,60]", "(60,70]", "(70,80]", "(80,90]", "(90,100]")
  
  bins10 <- data.frame(Bins = bins_width10, BinOrder = seq(1,21,1), stringsAsFactors = F)
  bins10 <- mutate(bins10, BinColor = ifelse(Bins=="0",1,0))
  
  #--- Determines the current proposal
  
  if(Proposal == "BilingualEduc")
  {
    propTitle <- "Bilingual Education"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["EducationPref"]]
    Sample_Data[["CurrentPref_Abstain"]] <- Sample_Data[["EducationPref_Abstain"]]
    Sample_Data[["CurrentPref_AbstainWP"]] <- Sample_Data[["EducationPref_AbstainWP"]]
  }
  if(Proposal == "Immigration")
  {
    propTitle <- "Immigration"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["ImmigrationPref"]]
    Sample_Data[["CurrentPref_Abstain"]] <- Sample_Data[["ImmigrationPref_Abstain"]]
    Sample_Data[["CurrentPref_AbstainWP"]] <- Sample_Data[["ImmigrationPref_AbstainWP"]]
  }
  if(Proposal == "TeacherTenure")
  {
    propTitle <- "Teacher Tenure"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["TeachersPref"]]
    Sample_Data[["CurrentPref_Abstain"]] <- Sample_Data[["TeachersPref_Abstain"]]
    Sample_Data[["CurrentPref_AbstainWP"]] <- Sample_Data[["TeachersPref_AbstainWP"]]
  }
  if(Proposal == "PublicBonds")
  {
    propTitle <- "Public Bonds"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["BondsPref"]]
    Sample_Data[["CurrentPref_Abstain"]] <- Sample_Data[["BondsPref_Abstain"]]
    Sample_Data[["CurrentPref_AbstainWP"]] <- Sample_Data[["BondsPref_AbstainWP"]]
  }
  
  #--- Prepate data
  
  ProposalPos <- filter(Sample_Data, CurrentPref > 0) %>% mutate(CurrentPref_Bins = cut(CurrentPref, right = T, breaks = seq(0, 100, binWidth)))
  ProposalNeg <- filter(Sample_Data, CurrentPref < 0) %>% mutate(CurrentPref_Bins = cut(CurrentPref, right = F, breaks = seq(-100, 0, binWidth)))
  ProposalZero <- filter(Sample_Data, CurrentPref == 0) %>% mutate(CurrentPref_Bins = "0")
  
  CurrentProp <- bind_rows(ProposalNeg, ProposalZero, ProposalPos) %>% select(MID, CurrentPref, CurrentPref_Bins)
  CurrentProp <- group_by(CurrentProp, CurrentPref_Bins) %>% summarise(NumSubjects = n()) %>% rename(Bins = CurrentPref_Bins)
  CurrentProp <- full_join(CurrentProp, bins10, by = "Bins")
  CurrentProp$Bins <- factor(CurrentProp$Bins, levels = CurrentProp$Bins[order(CurrentProp$BinOrder)])
  
  
  #--- Create plot

  intensityPlot <- ggplot(data = CurrentProp, aes(x=Bins, y = NumSubjects, fill=BinColor)) +
    geom_bar(stat="identity") + scale_y_continuous(limits = c(0,plotMax), breaks = seq(0, plotMax, by = 10)) +
    xlab("Number of Points Assigned to Proposal") + ylab("Number of Subjects") +
    theme(axis.text.x=element_text(size=12, vjust = -.05, angle = 90), legend.text=element_text(size=16), title=element_text(size=14)) +
    ggtitle(paste(SampleTitle, " Preference Histogram - ",propTitle, sep = "")) + guides(fill = F) +
    theme(plot.title = element_text(hjust = 0.5))
  
  #annotations
  refPosMean <- round(mean(ProposalPos$CurrentPref), digits = 2)
  refPosTotal <- sum(ProposalPos$CurrentPref)
  refNegMean <- abs(round(mean(ProposalNeg$CurrentPref), digits = 2))
  refNegTotal <- abs(sum(ProposalNeg$CurrentPref))
  totalAbstain <- sum(Sample_Data$CurrentPref_Abstain)
  totalAbstainWP <- sum(Sample_Data$CurrentPref_AbstainWP)
  
  
  
  # Here we determine the winning side based on simple majority
  
  if(SampleTitle == "SV")
  {
    winningSide <- Sample_Summary$WinningSide[Sample_Summary$Referenda == paste(Proposal,"_NoBV",sep = "")]
    
    l1 <- paste("Winning Side (Simple Maj): ", winningSide, sep = "")
    l2 <- paste("Points In Favor (Total/Mean): ",refPosTotal,"/",refPosMean, sep = "")
    l3 <- paste("Points Against (Total/Mean): ",refNegTotal,"/",refNegMean, sep = "")
    l4 <- paste("Votes on Abstain (Total/With Points): ",totalAbstain,"/",totalAbstainWP, sep = "")
    l5 <- paste("Votes In Favor (NSV/SV1/SV2): ", Sample_Summary$InFavor[Sample_Summary$Referenda == paste(Proposal,"_NoBV",sep = "")], 
                "/",Sample_Summary$InFavor[Sample_Summary$Referenda == paste(Proposal,"_withBV1",sep = "")],
                "/",Sample_Summary$InFavor[Sample_Summary$Referenda == paste(Proposal,"_withBV2",sep = "")], sep = "")
    l6 <- paste("Votes Against (NSV/SV1/SV2): ", Sample_Summary$Against[Sample_Summary$Referenda == paste(Proposal,"_NoBV",sep = "")], 
                "/",Sample_Summary$Against[Sample_Summary$Referenda == paste(Proposal,"_withBV1",sep = "")],
                "/",Sample_Summary$Against[Sample_Summary$Referenda == paste(Proposal,"_withBV2",sep = "")], sep = "") 
  }
  
  
  if(SampleTitle == "QV")
  {
    winningSide <- Sample_Summary$WinningSide[Sample_Summary$Referenda == Proposal]
    
    l1 <- paste("Winning Side (Simple Maj): ", winningSide, sep = "")
    l2 <- paste("Points In Favor (Total/Mean): ",refPosTotal,"/",refPosMean, sep = "")
    l3 <- paste("Points Against (Total/Mean): ",refNegTotal,"/",refNegMean, sep = "")
    l4 <- paste("Votes on Abstain (Total/With Points): ",totalAbstain,"/",totalAbstainWP, sep = "")
    l5 <- paste("Votes In Favor (NQV/QV1/QV2): ", Sample_Summary$InFavor[Sample_Summary$Referenda == Proposal], 
                "/",Sample_Summary$InFavor[Sample_Summary$Referenda == paste(Proposal,"_QV1",sep = "")],
                "/",Sample_Summary$InFavor[Sample_Summary$Referenda == paste(Proposal,"_QV2",sep = "")], sep = "")
    l6 <- paste("Votes Against (NQV/QV1/QV2): ", Sample_Summary$Against[Sample_Summary$Referenda == Proposal], 
                "/",Sample_Summary$Against[Sample_Summary$Referenda == paste(Proposal,"_QV1",sep = "")],
                "/",Sample_Summary$Against[Sample_Summary$Referenda == paste(Proposal,"_QV2",sep = "")], sep = "") 
  }
  
  
  intensityPlot <- intensityPlot + annotate("text", x = 17, y = c(70,66,62,58,54,50), label = c(l1,l2,l3,l4,l5,l6))
  print(intensityPlot)
  ggsave(intensityPlot, file=paste("California Stage 2 - Experiment Plots/Sample Preference Intensity Histograms/",SampleTitle," Preference Histogram - ", propTitle, ".png", sep = ""), height = 4.5, width = 9.5)
  
}


ProposalCDF <- function(Sample_Data, Proposal, SampleTitle)
{
  #--- Determines the current proposal
  
  if(Proposal == "BilingualEduc")
  {
    propTitle <- "Bilingual Education"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["EducationPref"]]
    Sample_Data[["CurrentProp"]] <- Sample_Data[["Ref1_BilingualEduc"]]
  }
  
  if(Proposal == "Immigration")
  {
    propTitle <- "Immigration"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["ImmigrationPref"]]
    Sample_Data[["CurrentProp"]] <- Sample_Data[["Ref4_Immigration"]]
  }
  
  if(Proposal == "TeacherTenure")
  {
    propTitle <- "Teacher Tenure"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["TeachersPref"]]
    Sample_Data[["CurrentProp"]] <- Sample_Data[["Ref2_TeacherTenure"]]
  }
  
  if(Proposal == "PublicBonds")
  {
    propTitle <- "Public Bonds"
    Sample_Data[["CurrentPref"]] <- Sample_Data[["BondsPref"]]
    Sample_Data[["CurrentProp"]] <- Sample_Data[["Ref3_PublicBonds"]]
  }
  
  Sample_Data <- filter(Sample_Data, CurrentProp != "Abstain") #remove abstain
  
  
  intensityCDF <- ggplot(data = Sample_Data, aes(x=CurrentPref, colour = CurrentProp)) + 
    stat_ecdf(size=.75) + scale_x_continuous(breaks = c(0,20,40,60,80,100)) + ylab("Proportion") + 
    xlab("Points Assigned to Proposal")  + theme_classic() + ggtitle(paste(SampleTitle, " Preference CDF - ", propTitle, sep = ""))+
    scale_colour_discrete(guide = guide_legend(title = NULL)) + theme(plot.title = element_text(hjust = 0.5))
  print(intensityCDF)
  
  ggsave(intensityCDF, file=paste("California Stage 2 - Experiment Plots/Sample Preference Intensity CDF/", SampleTitle," Preference CDF - ", propTitle, ".png", sep = ""), height = 4, width = 5)
  
}


####################################
######## SV Histograms (x4) ########
####################################

SV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean.csv", header = T, stringsAsFactors = F)
SV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/SV_VoteSummary.csv", header = T, stringsAsFactors = F)

# Creates columns to keep track of subjects who abstained and those who abstained AND assigned points (_AbstainWP). 
SV_data <- mutate(SV_data, TeachersPref_Abstain = ifelse(Ref2_TeacherTenure == "Abstain", 1, 0), 
                  BondsPref_Abstain = ifelse(Ref3_PublicBonds == "Abstain", 1, 0), 
                  EducationPref_Abstain = ifelse(Ref1_BilingualEduc == "Abstain", 1, 0), 
                  ImmigrationPref_Abstain = ifelse(Ref4_Immigration == "Abstain", 1, 0),
                  TeachersPref_AbstainWP = 0, BondsPref_AbstainWP = 0, EducationPref_AbstainWP = 0, ImmigrationPref_AbstainWP = 0)

for(row in 1:nrow(SV_data))
{
  ### Bilingual Education ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((SV_data$Ref1_BilingualEduc[row] == "InFavor") & (SV_data$EducationPref[row] == 0))
  {
    SV_data$EducationPref[row] <- 1
  }
  
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(SV_data$Ref1_BilingualEduc[row] == "Against")
  {
    SV_data$EducationPref[row] <- SV_data$EducationPref[row] * -1
    if(SV_data$EducationPref[row] == 0){SV_data$EducationPref[row] <- -1}
    
  } 
  
  # Counts number of Abstains with points and sets the preferences to zero. 
  if((SV_data$Ref1_BilingualEduc[row] == "Abstain") & (SV_data$EducationPref[row] > 0))
  {
    SV_data$EducationPref_AbstainWP[row] <- 1
    SV_data$EducationPref[row] <- 0
  }
  
  
  ### Teacher Tenure ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((SV_data$Ref2_TeacherTenure[row] == "InFavor") & (SV_data$TeachersPref[row] == 0)) # Only does something if its not in favor
  {
    SV_data$TeachersPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(SV_data$Ref2_TeacherTenure[row] == "Against")
  {
    SV_data$TeachersPref[row] <- SV_data$TeachersPref[row] * -1
    if(SV_data$TeachersPref[row] == 0){SV_data$TeachersPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((SV_data$Ref2_TeacherTenure[row] == "Abstain") & (SV_data$TeachersPref[row] > 0))
  {
    SV_data$TeachersPref_AbstainWP[row] <- 1
    SV_data$TeachersPref[row] <- 0
  }
  
  
  ### Public Bonds ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((SV_data$Ref3_PublicBonds[row] == "InFavor") & (SV_data$BondsPref[row] == 0)) # Only does something if its not in favor
  {
    SV_data$BondsPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(SV_data$Ref3_PublicBonds[row] == "Against")
  {
    SV_data$BondsPref[row] <- SV_data$BondsPref[row] * -1
    if(SV_data$BondsPref[row] == 0){SV_data$BondsPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((SV_data$Ref3_PublicBonds[row] == "Abstain") & (SV_data$BondsPref[row] > 0))
  {
    SV_data$BondsPref_AbstainWP[row] <- 1
    SV_data$BondsPref[row] <- 0
  }
  
  
  ### Immigration ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((SV_data$Ref4_Immigration[row] == "InFavor") & (SV_data$ImmigrationPref[row] == 0)) # Only does something if its not in favor
  {
    SV_data$ImmigrationPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(SV_data$Ref4_Immigration[row] == "Against")
  {
    SV_data$ImmigrationPref[row] <- SV_data$ImmigrationPref[row] * -1
    if(SV_data$ImmigrationPref[row] == 0){SV_data$ImmigrationPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((SV_data$Ref4_Immigration[row] == "Abstain") & (SV_data$ImmigrationPref[row] > 0))
  {
    SV_data$ImmigrationPref_AbstainWP[row] <- 1
    SV_data$ImmigrationPref[row] <- 0
  }
}

# Save recoded data with absolute values and exported
SV_data_allPositive <- mutate(SV_data, EducationPref = abs(EducationPref), TeachersPref = abs(TeachersPref), 
                              BondsPref = abs(BondsPref), ImmigrationPref = abs(ImmigrationPref))

write.csv(SV_data_allPositive, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_SV_Stage_2_clean&Recoded_AllPos.csv", row.names = F)



#--- Bilingual Educ

ProposalHist(SV_data, SV_summary, "BilingualEduc", "SV")
ProposalCDF(SV_data_allPositive, "BilingualEduc", "SV")

#--- Teacher Tenure

ProposalHist(SV_data, SV_summary, "TeacherTenure", "SV")
ProposalCDF(SV_data_allPositive, "TeacherTenure", "SV")

#--- Immigration

ProposalHist(SV_data, SV_summary, "Immigration", "SV")
ProposalCDF(SV_data_allPositive, "Immigration", "SV")

#--- Public Bonds

ProposalHist(SV_data, SV_summary, "PublicBonds", "SV")
ProposalCDF(SV_data_allPositive, "PublicBonds", "SV")


####################################
######## QV Histograms (x4) ########
####################################

QV_data <- read.csv("California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean.csv", header = T, stringsAsFactors = F)
QV_summary <- read.csv("California Stage 2 - SV & QV Data/Summaries & Tests/QV_VoteSummary.csv", header = T, stringsAsFactors = F)

# Creates columns to keep track of subjects who abstained and those who abstained AND assigned points (_AbstainWP). 
QV_data <- mutate(QV_data, TeachersPref_Abstain = ifelse(Ref2_TeacherTenure == "Abstain", 1, 0), 
                  BondsPref_Abstain = ifelse(Ref3_PublicBonds == "Abstain", 1, 0), 
                  EducationPref_Abstain = ifelse(Ref1_BilingualEduc == "Abstain", 1, 0), 
                  ImmigrationPref_Abstain = ifelse(Ref4_Immigration == "Abstain", 1, 0),
                  TeachersPref_AbstainWP = 0, BondsPref_AbstainWP = 0, EducationPref_AbstainWP = 0, ImmigrationPref_AbstainWP = 0)

for(row in 1:nrow(QV_data))
{
  ### Bilingual Education ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((QV_data$Ref1_BilingualEduc[row] == "InFavor") & (QV_data$EducationPref[row] == 0))
  {
    QV_data$EducationPref[row] <- 1
  }
  
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(QV_data$Ref1_BilingualEduc[row] == "Against")
  {
    QV_data$EducationPref[row] <- QV_data$EducationPref[row] * -1
    if(QV_data$EducationPref[row] == 0){QV_data$EducationPref[row] <- -1}
    
  } 
  
  # Counts number of Abstains with points and sets the preferences to zero. 
  if((QV_data$Ref1_BilingualEduc[row] == "Abstain") & (QV_data$EducationPref[row] > 0))
  {
    QV_data$EducationPref_AbstainWP[row] <- 1
    QV_data$EducationPref[row] <- 0
  }
  
  
  ### Teacher Tenure ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((QV_data$Ref2_TeacherTenure[row] == "InFavor") & (QV_data$TeachersPref[row] == 0)) # Only does something if its not in favor
  {
    QV_data$TeachersPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(QV_data$Ref2_TeacherTenure[row] == "Against")
  {
    QV_data$TeachersPref[row] <- QV_data$TeachersPref[row] * -1
    if(QV_data$TeachersPref[row] == 0){QV_data$TeachersPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((QV_data$Ref2_TeacherTenure[row] == "Abstain") & (QV_data$TeachersPref[row] > 0))
  {
    QV_data$TeachersPref_AbstainWP[row] <- 1
    QV_data$TeachersPref[row] <- 0
  }
  
  
  ### Public Bonds ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((QV_data$Ref3_PublicBonds[row] == "InFavor") & (QV_data$BondsPref[row] == 0)) # Only does something if its not in favor
  {
    QV_data$BondsPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(QV_data$Ref3_PublicBonds[row] == "Against")
  {
    QV_data$BondsPref[row] <- QV_data$BondsPref[row] * -1
    if(QV_data$BondsPref[row] == 0){QV_data$BondsPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((QV_data$Ref3_PublicBonds[row] == "Abstain") & (QV_data$BondsPref[row] > 0))
  {
    QV_data$BondsPref_AbstainWP[row] <- 1
    QV_data$BondsPref[row] <- 0
  }
  
  
  ### Immigration ###
  
  # If subject voted In Favor but assigned 0 pts, it changes it to 1 point. 
  if((QV_data$Ref4_Immigration[row] == "InFavor") & (QV_data$ImmigrationPref[row] == 0)) # Only does something if its not in favor
  {
    QV_data$ImmigrationPref[row] <- 1
  }
  # If subject voted Against, it makes the points negative. Corrects 0 pts to -1 point.
  if(QV_data$Ref4_Immigration[row] == "Against")
  {
    QV_data$ImmigrationPref[row] <- QV_data$ImmigrationPref[row] * -1
    if(QV_data$ImmigrationPref[row] == 0){QV_data$ImmigrationPref[row] <- -1}
    
  } 
  # Counts number of Abstains and sets the preferences to zero. 
  if((QV_data$Ref4_Immigration[row] == "Abstain") & (QV_data$ImmigrationPref[row] > 0))
  {
    QV_data$ImmigrationPref_AbstainWP[row] <- 1
    QV_data$ImmigrationPref[row] <- 0
  }
}


# Save recoded data with absolute values and exported
QV_data_allPositive <- mutate(QV_data, EducationPref = abs(EducationPref), TeachersPref = abs(TeachersPref), 
                              BondsPref = abs(BondsPref), ImmigrationPref = abs(ImmigrationPref))

write.csv(QV_data_allPositive, "California Stage 2 - SV & QV Data/Clean Data/California_MTurk_QV_Stage_2_clean&Recoded_AllPos.csv", row.names = F)



#--- Bilingual Educ

ProposalHist(QV_data, QV_summary, "BilingualEduc", "QV")
ProposalCDF(QV_data_allPositive, "BilingualEduc", "QV")

#--- Teacher Tenure

ProposalHist(QV_data, QV_summary, "TeacherTenure", "QV")
ProposalCDF(QV_data_allPositive, "TeacherTenure", "QV")

#--- Immigration

ProposalHist(QV_data, QV_summary, "Immigration", "QV")
ProposalCDF(QV_data_allPositive, "Immigration", "QV")

#--- Public Bonds

ProposalHist(QV_data, QV_summary, "PublicBonds", "QV")
ProposalCDF(QV_data_allPositive, "PublicBonds", "QV")


